<template>
<!-- 城市组件 -->
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggleDialog">
      <span class="value" v-if="fullLocation">{{fullLocation}}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="isShow">
        <!-- 加载转态 -->
      <div class="loading" v-if="isLoading"></div>
      <template v-else>
        <span class="ellipsis" v-for="item in currList" :key="item.code" @click="changeItem(item)">
          {{ item.name }}
          <!-- 北京 -->
        </span>
      </template>
    </div>
  </div>
</template>

<script>

import { onClickOutside } from '@vueuse/core'
import { ref, reactive, computed } from 'vue'
import axios from 'axios'

const getCityData = () => {
  // Promise 对象的 resolve 可以把值传出去
  return new Promise((resolve) => {
    if (window.cityData) {
      // 情况1:如果有内存缓存，返回缓存的数据即可
      resolve(window.cityData)
    } else {
      // 没有缓存，获取城市列表信息
      const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
      axios(url).then(({ data }) => {
        window.cityData = data
        // console.log(data)
        resolve(window.cityData)
      }
      )
    }
  })
}
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    // 控制展开收起,默认收起
    const isShow = ref(false)
    const isLoading = ref(false)
    const cityData = ref([])
    //   展开
    const show = () => {
      isShow.value = true
      isLoading.value = true
      //   发送请求
      getCityData().then((data) => {
        console.log(data)
        cityData.value = data
      })
      isLoading.value = false
      //   清空数据
      for (const key in address) {
        address[key] = ''
      }
    }

    const currList = computed(() => {
      // 默认情况下，是展示省份列表
      let list = cityData.value
      //   有省份的code
      if (address.provinceCode) {
        list = list.find(item => item.code === address.provinceCode).areaList
      }
      //   有市的code
      if (address.cityCode) {
        list = list.find(item => item.code === address.cityCode).areaList
      }
      return list
    })
    const hide = () => {
      isShow.value = false
    }
    const toggleDialog = () => {
      isShow.value === false ? show() : hide()
    }
    // 点击空白收起
    const target = ref(null)
    onClickOutside(target, () => {
      hide()
    })

    // 收货地址字段
    const address = reactive({
      fullLocation: '',
      provinceCode: '', // 省编码(后台需要)
      provinceName: '', // 省名称(显示需要)
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: ''
    })

    const changeItem = (item) => {
      if (item.level === 0) {
        address.provinceCode = item.code
        address.provinceName = item.name
      }
      if (item.level === 1) {
        address.cityCode = item.code
        address.cityName = item.name
      }
      if (item.level === 2) {
        address.countyCode = item.code
        address.countyName = item.name
        //   关闭模块
        hide()
        // 拼接地址信息
        address.fullLocation = `${address.provinceName} ${address.cityName} ${address.countyName}`
        emit('change', address)
      }
    }

    return { toggleDialog, target, isShow, isLoading, currList, changeItem }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    // 省略...
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
